<?php


namespace App\Services;


use App\Models\Department;
use App\Models\TeamGoals;
use App\Models\User;
use App\Service\Service;
use Carbon\Carbon;
use Dingo\Api\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
 * Class TeamGoalService
 * @property integer $department_id
 * @property integer $amount
 * @property integer $effect_mobile
 * @property integer $intention
 * @property integer $visit
 * @property integer $order
 * @property integer $wechat
 * @property integer $start
 * @property integer $end
 * @package App\Services
 * @author thanatos thanatos915@163.com
 */
class TeamGoalService extends Service
{

    /**
     * 团队目标列表
     * @param Request $request
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @author thanatos thanatos915@163.com
     */
    public static function search(Request $request)
    {
        $service = self::loadParams($request, [
            'department_id' => 'numeric',
        ]);

        $query = TeamGoals::with('user')->orderBy('created_at', 'desc');

        if ($service->department_id) {
            $query->whereIn('user_id', User::where('department_id', $service->department_id)->select('id'));
        }

        return $query->paginate();
    }

    /**
     * 批量创建目标
     * @author thanatos thanatos915@163.com
     */
    public static function batchCreate(Request $request)
    {
        $service = static::loadParams($request, [
            'department_id' => 'required',
            'amount' => 'required',
            'effect_mobile' => 'required',
            'intention' => 'required',
            'visit' => 'required',
            'order' => 'required',
            'wechat' => 'required',
            'month' => 'required',
        ]);

        if (!Department::whereId($service->department_id)->exists()) {
            throw new BadRequestHttpException("部门不存在");
        }

        // 判断是不是父部门
        $child = Department::whereParentId($service->department_id)->get();
        if ($child->count() > 0) {
            if (Department::whereIn('parent_id', $child->modelKeys())->count() > 0) {
                throw new BadRequestHttpException("部门层级过多，无法批量创建");
            }
            $users = User::whereIn('department_id', $child->modelKeys())->get();
        } else {
            $users = User::whereDepartmentId($service->department_id)->get();
        }

        if ($users->count() <= 0) {
            throw new BadRequestHttpException("部门人员为空");
        }

        $month = Carbon::createFromTimestamp(strtotime($service->month))->startOfMonth();

        if (TeamGoals::whereIn('user_id', $users->modelKeys())->where('month', $month)->exists()) {
            throw new BadRequestHttpException("该月份已创建过目标, 无法覆盖");
        }

        $insert = [];
        foreach ($users as $user) {
            array_push($insert, [
                'user_id' => $user->id,
                'month' => $month,
                'amount' => $service->amount,
                'effect_mobile' => $service->effect_mobile,
                'intention' => $service->intention,
                'visit' => $service->visit,
                'order' => $service->order,
                'wechat' => $service->wechat,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now()
            ]);
        }

        TeamGoals::insert($insert);

    }

    /**
     * 创建或修改目标
     * @author thanatos thanatos915@163.com
     */
    public static function createAndUpdate()
    {

    }

}
